$(function () {
    //定义校验的正则表达式
    let regExpByUname = /^[a-zA-Z]{6,16}.*$/;
    let regExpByPassword = /^[a-zA-z\d]\w{7,16}.*$/;
    $("#user").on("blur", function () {
        alert("点击了")
        let uname = $("#user").val().toString();
        if (uname === null || uname === "") {
            $("#tipMessage").text("账户名不能为空！").css("color", "red").css("colorFamily", "宋体").css("display", "block");
        } else if (!regExpByUname.test(uname)) {
            $("#tipMessage").text("账户名长度6-16位之间").css("color", "red").css("colorFamily", "宋体").css("display", "block");
        } else {
            $("#tipMessage").text("");
        }
    })

    $("#password").on("blur", function () {
        let password = $("#password").val().toString();
        if (password === null || password === "") {
            $("#tipMessage").text("密码不能为空").css("color", "red").css("colorFamily", "宋体").css("display", "block");
        } else if (!regExpByPassword.test(password)) {
            $("#tipMessage").text("密码长度在7-16").css("color", "red").css("colorFamily", "宋体").css("display", "block");
        } else {
            $("#tipMessage").text("");
        }
    })


    //键盘事件
    window.onkeyup = function ($event) {
        if ($event.keyCode === 13) {
            $.post("http://localhost:8080/Shop_war_exploded/users/login",
                {
                    uname: $('#user').val(),
                    upwd: $("#password").val(),
                },
                function (result) {
                    if (result === "success") {
                        self.location.href = "http://localhost:8080/Shop_war_exploded/users/loginSuccess";
                    } else {
                        $("#tipMessage").text("账户名或者密码错误！").css("color", "red").css("colorFamily", "宋体").css("display", "block");
                        $event.stopPropagation();
                    }
                }
            );
        }
    }

    //鼠标事件
    $("#login").unbind("click").bind("click", function ($event) {
        $.post("http://localhost:8080/Shop_war_exploded/users/login",
            {
                uname: $('#user').val(),
                upwd: $("#password").val(),
            },
            function (result) {
                if (result === "success") {
                    self.location.href = "http://localhost:8080/Shop_war_exploded/users/loginSuccess";
                } else {
                    $("#tipMessage").text("账户名或者密码错误！").css("color", "red").css("colorFamily", "宋体").css("display", "block");
                    $event.stopPropagation();
                }
            }
        );
    })
})